package id.co.rho.scoring.cid.client.widgets;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.HeaderGroupConfig;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Hidden;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: frizky
 * Date: 10/2/11
 * Time: 8:20 PM
 */
public class GridExcel2<M extends ModelData> extends Grid {
    FormPanel formPanel;
    private ToolBar toolBar;
    private Button btnExport;

    public GridExcel2(ListStore<M> listStore, ColumnModel cm) {
        super(listStore, cm);
        formPanel = new FormPanel();
        formPanel.setAction(GWT.getModuleBaseURL() + "exportExcel");
        formPanel.setMethod(FormPanel.Method.POST);
        formPanel.setVisible(false);

        final Hidden headerHidden = new Hidden("header");
        final Hidden fieldsHidden = new Hidden("fields");
        final Hidden contentHidden = new Hidden("content");
        formPanel.add(headerHidden);
        formPanel.add(fieldsHidden);
        formPanel.add(contentHidden);
        toolBar = new ToolBar();
        btnExport = new Button("Export");
        btnExport.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                List<ModelData> models = getStore().getModels();
                List<ColumnConfig> columns = getColumnModel().getColumns();
                JSONArray headerArray = new JSONArray();
                JSONArray fieldArray = new JSONArray();
                String[] fields = new String[columns.size()];
                int i = 0;
                for (ColumnConfig columnConfig : columns) {
                    headerArray.set(i, new JSONString(String.valueOf(columnConfig.getHeader())));
                    fieldArray.set(i, new JSONString(String.valueOf(columnConfig.getId())));

                    fields[i] = columnConfig.getId();
                    i++;
                }
                int indexModelsArray = 0;
                JSONArray modelsArray = new JSONArray();
                for (ModelData model : models) {
                    JSONArray modelArray = new JSONArray();
                    int indexModelArray = 0;
                    for (String strField : fields) {
                        modelArray.set(indexModelArray, new JSONString(String.valueOf(model.get(strField))));
                        indexModelArray++;
                    }
                    modelsArray.set(indexModelsArray, modelArray);
                    indexModelsArray++;
                }
                headerHidden.setValue(headerArray.toString());
                fieldsHidden.setValue(fieldArray.toString());
                contentHidden.setValue(modelsArray.toString());
                formPanel.submit();
            }
        });
        toolBar.add(btnExport);
        toolBar.add(formPanel);
        this.cm.addHeaderGroup(0, 0, new HeaderGroupConfig(toolBar, 1, cm.getColumnCount()));

//        this.store = listStore;
//        this.cm = cm;
//        this.view = new GridView();
//        disabledStyle = null;
//        baseStyle = "x-grid-panel";
//        setSelectionModel(new GridSelectionModel<M>());
//        disableTextSelection(true);
    }
}
